package l1j.server.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.List;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;
import l1j.server.server.utils.collections.Lists;
import l1j.server.server.utils.collections.Maps;

public class QuestTable {
	private static QuestTable _instance = new QuestTable();

	public static QuestTable getInstance() {
		return _instance;
	}

	public static void initilize() {
		_instance = new QuestTable();
	}

	private Map<Integer, List<AllQuestDetail>> _allQuest_String = Maps.newMap();

	private Map<Integer, AllQuestDetail> _allQuest_MobDrop_ById = Maps.newMap();
	
	private Map<Integer, Integer> _allQuest_MobDrop = Maps.newMap();

	private QuestTable() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM x_quest");

			rs = pstm.executeQuery();
			while (rs.next()) {
				AllQuestDetail qd = new AllQuestDetail();
				qd.setQuest_id(rs.getInt("quest_id"));
				qd.setQuest_name(rs.getString("quest_name"));
				qd.setQuest_level(rs.getInt("quest_level"));
				qd.setQuest_type(rs.getString("quest_type"));
				qd.setQuest_item_1(rs.getInt("quest_item_1"));
				qd.setQuest_item_1_count(rs.getInt("quest_item_1_count"));
				qd.setQuest_item_1_dropmob(rs.getInt("quest_item_1_dropmob"));
				qd.setQuest_item_2(rs.getInt("quest_item_2"));
				qd.setQuest_item_2_count(rs.getInt("quest_item_2_count"));
				qd.setQuest_item_2_dropmob(rs.getInt("quest_item_2_dropmob"));
				qd.setQuest_item_3(rs.getInt("quest_item_3"));
				qd.setQuest_item_3_count(rs.getInt("quest_item_3_count"));
				qd.setQuest_item_3_dropmob(rs.getInt("quest_item_3_dropmob"));
				qd.setQuest_award_1(rs.getInt("quest_award_1"));
				qd.setQuest_award_1_count(rs.getInt("quest_award_1_count"));
				qd.setQuest_award_2(rs.getInt("quest_award_2"));
				qd.setQuest_award_2_count(rs.getInt("quest_award_2_count"));
				qd.setQuest_award_3(rs.getInt("quest_award_3"));
				qd.setQuest_award_3_count(rs.getInt("quest_award_3_count"));
				qd.setQuest_string_1(rs.getString("quest_string_1"));
				qd.setQuest_string_2(rs.getString("quest_string_2"));
				qd.setQuest_string_3(rs.getString("quest_string_3"));
				qd.setQuest_string_4(rs.getString("quest_string_4"));
				qd.setQuest_string_5(rs.getString("quest_string_5"));
				qd.setQuest_string_6(rs.getString("quest_string_6"));
				qd.setQuest_string_7(rs.getString("quest_string_7"));
				qd.setQuest_string_8(rs.getString("quest_string_8"));
				qd.setQuest_string_9(rs.getString("quest_string_9"));
				qd.setQuest_string_10(rs.getString("quest_string_10"));
				int q_level = rs.getInt("quest_level");
				if (!_allQuest_String.containsKey(q_level)) {
					List<AllQuestDetail> list = Lists.newList();
					_allQuest_String.put(new Integer(q_level), list);
				}
				List<AllQuestDetail> list = _allQuest_String.get(q_level);
				list.add(qd);
				_allQuest_MobDrop_ById.put(rs.getInt("quest_id"), qd);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

	/**
	 * 取得指定任務ID清單 <0,3,6>第1~3任務怪 <1,4,7>第1~3任務道具 <2,5,8>第1~3任務道具數量
	 * <9,11,13>第1~3任務獎勵 <10,12,14>第1~3任務獎勵數量
	 **/
	public List<Integer> getQuestNewDetail(int quest_id) {
		List<Integer> questNewMobDrop = Lists.newList();
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_item_1_dropmob());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_item_1());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_item_1_count());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_item_2_dropmob());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_item_2());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_item_2_count());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_item_3_dropmob());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_item_3());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_item_3_count());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_award_1());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_award_1_count());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_award_2());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_award_2_count());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_award_3());
		questNewMobDrop.add(_allQuest_MobDrop_ById.get(quest_id)
				.getQuest_award_3_count());
		return questNewMobDrop;
	}

	public Map<Integer, Integer> getAllQuestMobDrop(List<Integer> questActionId) {
		_allQuest_MobDrop.clear();
		for (int i = 0; i < questActionId.size(); i++) {
			if (checkAllQuestMobDrop(questActionId.get(i))) {
				_allQuest_MobDrop.put(
						_allQuest_MobDrop_ById.get(questActionId.get(i))
								.getQuest_item_1_dropmob(),
						_allQuest_MobDrop_ById.get(questActionId.get(i))
								.getQuest_item_1());
				_allQuest_MobDrop.put(
						_allQuest_MobDrop_ById.get(questActionId.get(i))
								.getQuest_item_2_dropmob(),
						_allQuest_MobDrop_ById.get(questActionId.get(i))
								.getQuest_item_2());
				_allQuest_MobDrop.put(
						_allQuest_MobDrop_ById.get(questActionId.get(i))
								.getQuest_item_3_dropmob(),
						_allQuest_MobDrop_ById.get(questActionId.get(i))
								.getQuest_item_3());
			}
		}
		return _allQuest_MobDrop;
	}

	public boolean checkAllQuestMobDrop(int quest_id) {
		if (!_allQuest_MobDrop_ById.containsKey(quest_id)) {
			return false;
		}
		return true;
	}

	public List<AllQuestDetail> getQuestString(int quest_level) {
		if (!_allQuest_String.containsKey(quest_level)) {
			return Lists.newList();
		}
		return _allQuest_String.get(quest_level);
	}

	public class AllQuestDetail {
		private int quest_id = 0;
		private String quest_name = "";
		private int quest_level = 0;
		private String quest_type = "";
		private int quest_item_1 = 0;
		private int quest_item_1_count = 0;
		private int quest_item_1_dropmob = 0;
		private int quest_item_2 = 0;
		private int quest_item_2_count = 0;
		private int quest_item_2_dropmob = 0;
		private int quest_item_3 = 0;
		private int quest_item_3_count = 0;
		private int quest_item_3_dropmob = 0;
		private int quest_award_1 = 0;
		private int quest_award_1_count = 0;
		private int quest_award_2 = 0;
		private int quest_award_2_count = 0;
		private int quest_award_3 = 0;
		private int quest_award_3_count = 0;
		private String quest_string_1 = "";
		private String quest_string_2 = "";
		private String quest_string_3 = "";
		private String quest_string_4 = "";
		private String quest_string_5 = "";
		private String quest_string_6 = "";
		private String quest_string_7 = "";
		private String quest_string_8 = "";
		private String quest_string_9 = "";
		private String quest_string_10 = "";

		public int getQuest_id() {
			return quest_id;
		}

		public void setQuest_id(int quest_id) {
			this.quest_id = quest_id;
		}

		public String getQuest_name() {
			return quest_name;
		}

		public void setQuest_name(String quest_name) {
			this.quest_name = quest_name;
		}

		public int getQuest_level() {
			return quest_level;
		}

		public void setQuest_level(int quest_level) {
			this.quest_level = quest_level;
		}

		public String getQuest_type() {
			return quest_type;
		}

		public void setQuest_type(String quest_type) {
			this.quest_type = quest_type;
		}

		public int getQuest_item_1() {
			return quest_item_1;
		}

		public void setQuest_item_1(int quest_item_1) {
			this.quest_item_1 = quest_item_1;
		}

		public int getQuest_item_1_count() {
			return quest_item_1_count;
		}

		public void setQuest_item_1_count(int quest_item_1_count) {
			this.quest_item_1_count = quest_item_1_count;
		}

		public int getQuest_item_1_dropmob() {
			return quest_item_1_dropmob;
		}

		public void setQuest_item_1_dropmob(int quest_item_1_dropmob) {
			this.quest_item_1_dropmob = quest_item_1_dropmob;
		}

		public int getQuest_item_2() {
			return quest_item_2;
		}

		public void setQuest_item_2(int quest_item_2) {
			this.quest_item_2 = quest_item_2;
		}

		public int getQuest_item_2_count() {
			return quest_item_2_count;
		}

		public void setQuest_item_2_count(int quest_item_2_count) {
			this.quest_item_2_count = quest_item_2_count;
		}

		public int getQuest_item_2_dropmob() {
			return quest_item_2_dropmob;
		}

		public void setQuest_item_2_dropmob(int quest_item_2_dropmob) {
			this.quest_item_2_dropmob = quest_item_2_dropmob;
		}

		public int getQuest_item_3() {
			return quest_item_3;
		}

		public void setQuest_item_3(int quest_item_3) {
			this.quest_item_3 = quest_item_3;
		}

		public int getQuest_item_3_count() {
			return quest_item_3_count;
		}

		public void setQuest_item_3_count(int quest_item_3_count) {
			this.quest_item_3_count = quest_item_3_count;
		}

		public int getQuest_item_3_dropmob() {
			return quest_item_3_dropmob;
		}

		public void setQuest_item_3_dropmob(int quest_item_3_dropmob) {
			this.quest_item_3_dropmob = quest_item_3_dropmob;
		}

		public int getQuest_award_1() {
			return quest_award_1;
		}

		public void setQuest_award_1(int quest_award_1) {
			this.quest_award_1 = quest_award_1;
		}

		public int getQuest_award_1_count() {
			return quest_award_1_count;
		}

		public void setQuest_award_1_count(int quest_award_1_count) {
			this.quest_award_1_count = quest_award_1_count;
		}

		public int getQuest_award_2() {
			return quest_award_2;
		}

		public void setQuest_award_2(int quest_award_2) {
			this.quest_award_2 = quest_award_2;
		}

		public int getQuest_award_2_count() {
			return quest_award_2_count;
		}

		public void setQuest_award_2_count(int quest_award_1_count) {
			this.quest_award_2_count = quest_award_1_count;
		}

		public int getQuest_award_3() {
			return quest_award_3;
		}

		public void setQuest_award_3(int quest_award_3) {
			this.quest_award_3 = quest_award_3;
		}

		public int getQuest_award_3_count() {
			return quest_award_3_count;
		}

		public void setQuest_award_3_count(int quest_award_3_count) {
			this.quest_award_3_count = quest_award_3_count;
		}

		public String getQuest_String_1() {
			return quest_string_1;
		}

		public void setQuest_string_1(String quest_string_1) {
			this.quest_string_1 = quest_string_1;
		}

		public String getQuest_String_2() {
			return quest_string_2;
		}

		public void setQuest_string_2(String quest_string_2) {
			this.quest_string_2 = quest_string_2;
		}

		public String getQuest_String_3() {
			return quest_string_3;
		}

		public void setQuest_string_3(String quest_string_3) {
			this.quest_string_3 = quest_string_3;
		}

		public String getQuest_String_4() {
			return quest_string_4;
		}

		public void setQuest_string_4(String quest_string_4) {
			this.quest_string_4 = quest_string_4;
		}

		public String getQuest_String_5() {
			return quest_string_5;
		}

		public void setQuest_string_5(String quest_string_5) {
			this.quest_string_5 = quest_string_5;
		}

		public String getQuest_String_6() {
			return quest_string_6;
		}

		public void setQuest_string_6(String quest_string_6) {
			this.quest_string_6 = quest_string_6;
		}

		public String getQuest_String_7() {
			return quest_string_7;
		}

		public void setQuest_string_7(String quest_string_7) {
			this.quest_string_7 = quest_string_7;
		}

		public String getQuest_String_8() {
			return quest_string_8;
		}

		public void setQuest_string_8(String quest_string_8) {
			this.quest_string_8 = quest_string_8;
		}

		public String getQuest_String_9() {
			return quest_string_9;
		}

		public void setQuest_string_9(String quest_string_9) {
			this.quest_string_9 = quest_string_9;
		}

		public String getQuest_String_10() {
			return quest_string_10;
		}

		public void setQuest_string_10(String quest_string_10) {
			this.quest_string_10 = quest_string_10;
		}
	}
}